package net.jumpengine.ui;

import java.util.List;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import net.jumpengine.Manager;
import net.jumpengine.bean.Control;
import net.jumpengine.inter.IMouseEvent;
import net.jumpengine.po.Border;
import net.jumpengine.util.UComm;
import net.jumpengine.util.UDraw;

/**
  * 复选框, height只是规定一行的高度, 当文字宽度大于width-复选框宽度-3时,会分多行显示
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020年12月8日 下午8:08:40<br />
 * @date
 */
public class CheckBox extends Control {
	protected boolean value=false;
	protected Color selectBack=Color.web("0075FF"),tickColor=Color.WHITE;
	protected int fontSize;
	protected int padding=3;
	private int fx,fontWidth,fontHeight;
	private List<String> strs;
	private double[] points=new double[] {0.061538,0.425641,0.394871,0.666666,0.961538,0.123076,0.961538,0.402564,0.407692,0.902564,0.061538,0.635897};
	private double[] pointMath=new double[12];
	private IMouseEvent mouseEvent;
	
	public CheckBox(int x, int y, int width, int fontSize,String text) {
		this(x, y, width, fontSize,0, text);
	}

	public CheckBox(int x, int y, int width, int fontSize, int index,String text) {
		super(x, y, width, fontSize, index, null);
		this.fontSize=fontSize;
		int line,radius;
		if(fontSize<=20) {
			line=2;radius=4;
		}else if(fontSize<=40) {
			line=4;radius=4;
		}else {
			line=6;radius=6;
		}
		border=new Border(line, radius);
		border.setColor(Color.web("DDDDDD"));
		this.text=text;
		color=Manager.getColor();
		font=Font.font(fontSize);
	}

	@Override
	public void draw(GraphicsContext g) {
		g.setStroke(border.getColor());
		g.setLineWidth(border.getLeft());
		g.strokeRoundRect(x, y, fontSize, fontSize, border.getRadius(), border.getRadius());
		if(change) {
			change=false;
			fontWidth=width-fontSize-padding;
			fx=x+fontSize+padding;
			strs=UDraw.splitString(text, fontWidth, font);
			int count=UComm.size(strs);
			fontHeight=(count==0?fontSize:count*fontSize)+(count==0?0:(count-1)*padding);
			height=fontHeight;
			for(int i=0;i<points.length;i++) {
				if(i % 2==0) {
					pointMath[i]=points[i]*fontSize+x;
				}else {
					pointMath[i]=points[i]*fontSize+y;
				}
			}
		}
		if(value) {
			int line=border.getLeft()-2>0?border.getLeft()-2:border.getLeft()-1,dbl=line*2;
			g.setFill(selectBack);
			g.fillRoundRect(x+line, y+line, fontSize-dbl, fontSize-dbl, border.getRadius(), border.getRadius());
			
			g.setFill(tickColor);
			g.beginPath();
			for(int i=0;i<pointMath.length;i++) {
				if(i % 2==1)continue;
				if(i==0) {
					g.moveTo(pointMath[i], pointMath[i+1]);
				}else {
					g.lineTo(pointMath[i], pointMath[i+1]);
				}
			}
			g.closePath();
			g.fill();
		}
		
		g.setFill(color);
		g.setFont(font);
		
		UDraw.drawStrings(g, strs, fx, y, padding);
	}
	
	@Override
	public IMouseEvent getMouseEvent() {
		if(mouseEvent==null) {
			mouseEvent=new IMouseEvent() {
				@Override
				public void click(int x, int y, MouseButton button) {
					CheckBox.this.noValue();
				}
				
				@Override
				public void mouseReleased(int x, int y, MouseButton button) { }
				
				@Override
				public void mousePressed(int x, int y, MouseButton button) { }
				
				@Override
				public void mouseMove(int x, int y, MouseButton button) { }
			};
		}
		return mouseEvent;
	}

	public boolean isValue() {
		return value;
	}

	public void setValue(boolean value) {
		this.value = value;
	}
	
	public void noValue() {
		value=!value;
	}

	public Color getSelectBack() {
		return selectBack;
	}

	public void setSelectBack(Color selectBack) {
		this.selectBack = selectBack;
	}

	public Color getTickColor() {
		return tickColor;
	}

	public void setTickColor(Color tickColor) {
		this.tickColor = tickColor;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
		change=true;
	}
	
	@Override
	public void setX(int x) {
		this.x=x;
		change=true;
	}
	
	@Override
	public void setY(int y) {
		this.y=y;
		change=true;
	}
	
	@Override
	public void setLocation(int x, int y) {
		super.setLocation(x, y);
		change=true;
	}

	public int getPadding() {
		return padding;
	}

	public void setPadding(int padding) {
		this.padding = padding;
		change=true;
	}
	
}
